package com.springboot.chongwu.controller.app;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.chongwu.entity.Pet;
import com.springboot.chongwu.entity.Sheying;
import com.springboot.chongwu.entity.model.Customer;
import com.springboot.chongwu.entity.view.SheyingView;
import com.springboot.chongwu.qpage.QPage;
import com.springboot.chongwu.service.ISheyingService;
import com.springboot.chongwu.util.ExcelMergeCellHandler;
import com.springboot.chongwu.util.ResultView;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 摄影 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-09-25
 */
@RestController
@RequestMapping("/api/front/sheying")
public class AppSheyingController {

    @Autowired
    private ISheyingService service;

    /**
     * 获取宠物列表
     * @return
     */
    @GetMapping("/getSelect")
    public ResultView getSelect() {
        List<SheyingView> sysJydzList=this.service.getSelectList();
        return ResultView.ok(sysJydzList);
    }
    /**
     * 根据ID获取当前数据
     * @return
     */
    @GetMapping("/getById")
    public ResultView getById(@NotNull(message = "ID不能为空") String id) {
        return ResultView.ok(this.service.getById(id));
    }


    /**
     * 测试1
     * @param id
     * @return
     */
    @GetMapping("/test")
    public ResultView test(String id) {
        Sheying sheying=this.service.getById(id);
        sheying.setImgcontent("test3");
        this.service.updateById(sheying);
        return ResultView.ok();
    }

    /**
     * 测试2
     * @return
     */
    @GetMapping("/test2")
    public ResultView test2(String id) {
        Sheying sheying=this.service.getById(id);
        sheying.setUtime(new Date());
        this.service.updateById(sheying);
        return ResultView.ok();
    }

    /**
     * 测试3
     * @return
     */
    @GetMapping("/test3")
    public ResultView test3(String id) {
        Sheying sheying=this.service.getById(id);
        Sheying up=new Sheying();
        up.setId(sheying.getId());
        up.setUtime(new Date());
        sheying.setImgcontent("");
        this.service.updateById(sheying);
        return ResultView.ok();
    }


    /**
     * 多级表头
     * @param response
     * @throws Exception
     */
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception {
        //模拟查询需要导出的数据
        //List result = getData();
        List<Customer> data = new ArrayList<>();
        Customer customer = new Customer("JiangXi", "江西电信公司", "1", "2", "3", "4", "5", "张三", "12345678910");
        data.add(customer);
        Customer customer2 = new Customer("JiangXi", "江西电信公司", "1", "2", "3", "4", "5", "李四", "15848563521");
        data.add(customer2);
        Customer customer3 = new Customer("GuangDong", "广东电信公司", "1", "2", "3", "4", "5", "小明", "15847953624");
        data.add(customer3);
        Customer customer4 = new Customer("GuangDong", "广东电信公司", "1", "2", "3", "4", "5", "小红", "16849531548");
        data.add(customer4);
        Customer customer5 = new Customer("GuangDong", "广东电信公司", "1", "2", "3", "4", "5", "小华", "16985632481");
        data.add(customer5);
        Customer customer6 = new Customer("BeiJing", "北京电信公司", "1", "2", "3", "4", "5", "姜维", "16598645874");
        data.add(customer6);
        // 1设置表头样式
        WriteCellStyle headStyle = new WriteCellStyle();
        // 1.1设置表头数据居中
        headStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 2设置表格内容样式
        WriteCellStyle bodyStyle = new WriteCellStyle();
        // 2.1设置表格内容水平居中
        bodyStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 2.2设置表格内容垂直居中
        bodyStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        // 3设置表格sheet样式
        WriteSheet sheet = EasyExcel.writerSheet("客户信息").head(Customer.class).sheetNo(1).build();
        // 4拿到表格处理对象
        ExcelWriter writer = EasyExcel.write(response.getOutputStream()).needHead(true).excelType(ExcelTypeEnum.XLSX)
                // 设置需要待合并的行和列。参数1：数值数组，指定需要合并的列；参数2：数值，指定从第几行开始合并
                .registerWriteHandler(new ExcelMergeCellHandler(new int[]{0, 1, 2, 3,4,5,7}, 0))
                // 设置单元格的风格样式
                .registerWriteHandler(new HorizontalCellStyleStrategy(headStyle, bodyStyle))
                .build();

        // 5写入excel数据
        writer.write(data, sheet);
        // 6通知浏览器以附件的形式下载处理，设置返回头要注意文件名有中文
        response.setHeader("Content-disposition", "attachment;filename=" + new String("客户信息表".getBytes("gb2312"), "ISO8859-1") + ".xlsx");
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        writer.finish();
    }


}
